package cn.swunet.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;

/**
 * 
 * @author: arosen.swunet@qq.com
 *
 * @date: 2020年4月23日
 *
 * @TDDO: 创建druid数据库连接池
 */
public class DBTools{
	private static DataSource dataSource = null;

	static {
		//加载配置文件
		Properties p = new Properties();
		try {
			p.load(DBTools.class.getClassLoader().getResourceAsStream("druid.properties"));
			//创建数据源对象
			dataSource = new DruidDataSource();
			//设置属性
			DruidDataSource ds = (DruidDataSource)dataSource;
			ds.setUrl(p.getProperty("jdbcUrl"));
			ds.setUsername(p.getProperty("username"));
			ds.setPassword(p.getProperty("password"));
			ds.setInitialSize(new Integer(p.getProperty("initialSize")));
			ds.setMaxActive(new Integer(p.getProperty("maxActive")));
			ds.setMinIdle(new Integer(p.getProperty("minIdle")));
			ds.setMaxWait(new Long(p.getProperty("maxWait")));
		} catch (IOException e) {
			System.out.println("加载druid.properties出错");
			System.exit(0);
		}
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:Connection
	 * @throws SQLException 
	 * @TODO:返回一个连接
	 */
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:DataSource
	 * @TODO:返回datasource
	 */
	public static DataSource getDataSource() {
		return dataSource;
	}
}
